unit Events;

interface

uses
  Database, EventQ, Functions, VarGrabber, CreateNewEvent, Sounds, Groups, AM, Vars,
  SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls, refactorings,
  Forms, Dialogs, DB, DBTables, StdCtrls, Buttons,
  ExtCtrls, Mask, Menus, ComCtrls, Grids,
  SelectGroup, DBCtrls, DBGrids, SMDBGrid;

type
  TFormEvents = class(TForm)
    TabEvents: TPageControl;
    PageEvents: TTabSheet;
    PageStatus: TTabSheet;
    BoxLow: TGroupBox;
    g45: TImage;
    x4: TImage;
    y4: TImage;
    z4: TImage;
    ImageEnemy4: TImage;
    Label6: TLabel;
    Label7: TLabel;
    s11: TEdit;
    s12: TEdit;
    s13: TEdit;
    s14: TEdit;
    s15: TEdit;
    a11: TEdit;
    a12: TEdit;
    a13: TEdit;
    a14: TEdit;
    a15: TEdit;
    bc11: TBitBtn;
    bc12: TBitBtn;
    bc13: TBitBtn;
    bc14: TBitBtn;
    bc15: TBitBtn;
    bs11: TBitBtn;
    bs12: TBitBtn;
    bs13: TBitBtn;
    bs14: TBitBtn;
    bs15: TBitBtn;
    p15: TBitBtn;
    p14: TBitBtn;
    p13: TBitBtn;
    p12: TBitBtn;
    p11: TBitBtn;
    BoxEvents: TGroupBox;
    BoxHigh: TGroupBox;
    Image16: TImage;
    Image17: TImage;
    Image18: TImage;
    Image19: TImage;
    Image20: TImage;
    Label19: TLabel;
    Label20: TLabel;
    s21: TEdit;
    s22: TEdit;
    s23: TEdit;
    s24: TEdit;
    s25: TEdit;
    a21: TEdit;
    a22: TEdit;
    a23: TEdit;
    a24: TEdit;
    a25: TEdit;
    bc21: TBitBtn;
    bc22: TBitBtn;
    bc23: TBitBtn;
    bc24: TBitBtn;
    bc25: TBitBtn;
    bs21: TBitBtn;
    bs22: TBitBtn;
    bs23: TBitBtn;
    bs24: TBitBtn;
    bs25: TBitBtn;
    p25: TBitBtn;
    p24: TBitBtn;
    p23: TBitBtn;
    p22: TBitBtn;
    p21: TBitBtn;
    BtnDeleteEvent: TBitBtn;
    MainMenu1: TMainMenu;
    MenuHelp: TMenuItem;
    PanelGroup: TPanel;
    Label2: TLabel;
    BtnPrior: TBitBtn;
    BtnNext: TBitBtn;
    BtnClose2: TBitBtn;
    BtnHelp2: TBitBtn;
    cs21: TBitBtn;
    cs22: TBitBtn;
    cs24: TBitBtn;
    cs23: TBitBtn;
    cs25: TBitBtn;
    cs11: TBitBtn;
    cs12: TBitBtn;
    cs13: TBitBtn;
    cs14: TBitBtn;
    cs15: TBitBtn;
    EditVars: TEdit;
    LabelVars: TLabel;
    ImagePointer: TImage;
    TimerPointer: TTimer;
    PanelMove: TPanel;
    BtnUp: TBitBtn;
    BtnDown: TBitBtn;
    MenuCtrlF: TMenuItem;
    BtnClose: TBitBtn;
    BtnHelp: TBitBtn;
    BtnQuickEvent: TBitBtn;
    MenuCtrlR: TMenuItem;
    PopupEvent: TPopupMenu;
    PopupDisable: TMenuItem;
    BtnDisable: TBitBtn;
    BtnEnable: TBitBtn;
    Bevel3: TBevel;
    CheckEventSounds: TCheckBox;
    CheckLowerEvent: TCheckBox;
    Panel1: TPanel;
    Status: TLabel;
    Script: TMemo;
    Bevel1: TBevel;
    Bevel2: TBevel;
    BtnClear: TBitBtn;
    BtnFunctions: TBitBtn;
    BtnVar: TBitBtn;
    BtnUndo: TBitBtn;
    BtnAddEvent: TBitBtn;
    BtnEdit: TBitBtn;
    Bevel4: TBevel;
    Label1: TLabel;
    LabelLines: TLabel;
    CtrlZ1: TMenuItem;
    EditCopy: TEdit;
    BtnGroups: TSpeedButton;
    BtnAM: TBitBtn;
    CtrlA1: TMenuItem;
    Invisible21: TMenuItem;
    Esc2: TMenuItem;
    BtnVariables: TBitBtn;
    PopupGroup: TMenuItem;
    PopupBASE: TMenuItem;
    g1: TMenuItem;
    g2: TMenuItem;
    g3: TMenuItem;
    g4: TMenuItem;
    g5: TMenuItem;
    g6: TMenuItem;
    g7: TMenuItem;
    g8: TMenuItem;
    g9: TMenuItem;
    g10: TMenuItem;
    g11: TMenuItem;
    g12: TMenuItem;
    g13: TMenuItem;
    g14: TMenuItem;
    g15: TMenuItem;
    g16: TMenuItem;
    g17: TMenuItem;
    g18: TMenuItem;
    g19: TMenuItem;
    g20: TMenuItem;
    N1: TMenuItem;
    BtnCopy: TBitBtn;
    DSEvents: TDataSource;
    DSGroups: TDataSource;
    EditGroup: TDBEdit;
    GridEvents: TSMDBGrid;
    procedure BtnOKClick(Sender: TObject);
    procedure FormMouseMove(Sender: TObject; Shift: TShiftState; X,
      Y: Integer);
    procedure BtnPlaySoundClick(Sender: TObject);
    procedure BtnDeleteEventClick(Sender: TObject);
    procedure BtnSetSoundClick(Sender: TObject);
    procedure bc11Click(Sender: TObject);
    procedure BtnHelpClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure BtnPriorClick(Sender: TObject);
    procedure DSGroupsDataChange(Sender: TObject; Field: TField);
    procedure TabEventsChanging(Sender: TObject; var AllowChange: Boolean);
    procedure FormKeyDown(Sender: TObject; var Key: Word;
      Shift: TShiftState);
    procedure DSEventsDataChange(Sender: TObject; Field: TField);
    procedure BtnClearBeepClick(Sender: TObject);
    procedure TimerPointerTimer(Sender: TObject);
    procedure BtnUpClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure BtnQuickEventClick(Sender: TObject);
    procedure PopupDisableClick(Sender: TObject);
    procedure PopupEventPopup(Sender: TObject);
    procedure BtnDisableClick(Sender: TObject);
    procedure ScriptChange(Sender: TObject);
    procedure BtnUndoClick(Sender: TObject);
    procedure BtnFunctionsClick(Sender: TObject);
    procedure BtnVarClick(Sender: TObject);
    procedure BtnClearClick(Sender: TObject);
    procedure BtnAddEventClick(Sender: TObject);
    procedure GridEventsEnter(Sender: TObject);
    procedure GridEventsKeyPress(Sender: TObject; var Key: Char);
    procedure GridEventsDblClick(Sender: TObject);
    procedure BtnGroupsClick(Sender: TObject);
    procedure BtnAMClick(Sender: TObject);
    procedure BtnVariablesClick(Sender: TObject);
    procedure PopupBASEClick(Sender: TObject);
    procedure BtnCopyClick(Sender: TObject);
    procedure GridEventsGetCellParams(Sender: TObject; Field: TField;
      AFont: TFont; var Background: TColor; Highlight: Boolean);
  private
    { Private declarations }
    procedure LoadStatusEvents;
    procedure SaveStatusEvents;
    procedure LoadStatusEvent(kind: integer; var action: string; var sound: string);
    procedure SaveStatusEvent(kind: integer; action, sound: string);
    procedure UpdatePointer;
    procedure CheckSave;
    function CountEvents(grp: string): integer;
  public
    { Public declarations }
  end;

var
  FormEvents: TFormEvents;
  bypass: boolean;

implementation

uses
  Main, DBClient,
  PortalGrid;

{$R *.DFM}

procedure TFormEvents.FormCreate(Sender: TObject);
begin
  bypass:=false;
end;

procedure TFormEvents.BtnOKClick(Sender: TObject);
begin
  CheckSave;
  SaveStatusEvents;
  Datawell.lastgroup:=EditGroup.text;
  Datawell.lastevent:=Datawell.TableEvents.fieldbyname('FEvent').asstring;
  close;
end;

procedure TFormEvents.FormMouseMove(Sender: TObject; Shift: TShiftState; X,
  Y: Integer);
begin
  Status.caption:=' '+(sender as TControl).hint;
end;

procedure TFormEvents.GridEventsGetCellParams(Sender: TObject; Field: TField;
  AFont: TFont; var Background: TColor; Highlight: Boolean);
begin
  if Field.fieldname = 'FEVENT' then begin
    if (Datawell.TableEvents.fieldbyname('FColor').asinteger = 0) and
      (Datawell.TableEvents.fieldbyname('BColor').asinteger = 0) then begin
      AFont.color:=clblack;
      Background:=clwindow;
    end
    else begin
      AFont.color:=Datawell.TableEvents.fieldbyname('FColor').asinteger;
      Background:=Datawell.TableEvents.fieldbyname('BColor').asinteger;
    end;
    if copy(Datawell.TableEvents.fieldbyname('FAction').asstring,1,1) = '|' then begin
      if Background <> clsilver then AFont.color:=clsilver
      else AFont.color:=clgray;
      AFont.style:=[fsitalic];
    end;
  end
  else begin
    if highlight then begin
      AFont.color:=clhighlighttext;
      Background:=clhighlight;
      if copy(Datawell.TableEvents.fieldbyname('FAction').asstring,1,1) = '|' then
        Background:=clblack;
    end
    else begin
      AFont.color:=clblack;
      Background:=clwindow;
      if copy(Datawell.TableEvents.fieldbyname('FAction').asstring,1,1) = '|' then
        Background:=clgray;
    end;
  end;
end;

procedure TFormEvents.BtnPlaySoundClick(Sender: TObject);
var sfile: string;
begin
  case (sender as TBitBtn).tag of
    11: sfile:=s11.hint;
    12: sfile:=s12.hint;
    13: sfile:=s13.hint;
    14: sfile:=s14.hint;
    15: sfile:=s15.hint;
    21: sfile:=s21.hint;
    22: sfile:=s22.hint;
    23: sfile:=s23.hint;
    24: sfile:=s24.hint;
    25: sfile:=s25.hint;
  end;
  FormMain.PlaySound(sfile,1,0,0,false);
end;

procedure TFormEvents.BtnDeleteEventClick(Sender: TObject);
begin
  if not Datawell.TableEvents.EOF then Datawell.TableEvents.delete;
  TimerPointer.enabled:=true;
end;

procedure TFormEvents.BtnSetSoundClick(Sender: TObject);
var
  fullfile, work, sound: string;
  i: integer;
  form: TFormSounds;
begin
  form:=TFormSounds.create(self);
  form.showmodal;
  fullfile:=form.sound;
  form.free;
  if fullfile = '' then exit;
  work:=fullfile;
  delete(work,length(work)-3,4);
  i:=length(work);
  sound:='';
  while work[i] <> '\' do begin
    sound:=work[i]+sound;
    dec(i);
  end;
  case (sender as TBitBtn).tag of
    11: begin s11.text:=sound; s11.hint:=fullfile; end;
    12: begin s12.text:=sound; s12.hint:=fullfile; end;
    13: begin s13.text:=sound; s13.hint:=fullfile; end;
    14: begin s14.text:=sound; s14.hint:=fullfile; end;
    15: begin s15.text:=sound; s15.hint:=fullfile; end;
    21: begin s21.text:=sound; s21.hint:=fullfile; end;
    22: begin s22.text:=sound; s22.hint:=fullfile; end;
    23: begin s23.text:=sound; s23.hint:=fullfile; end;
    24: begin s24.text:=sound; s24.hint:=fullfile; end;
    25: begin s25.text:=sound; s25.hint:=fullfile; end;
  end;
end;

procedure TFormEvents.bc11Click(Sender: TObject);
begin
  case (sender as TBitBtn).tag of
    11: a11.text:='';
    12: a12.text:='';
    13: a13.text:='';
    14: a14.text:='';
    15: a15.text:='';
    21: a21.text:='';
    22: a22.text:='';
    23: a23.text:='';
    24: a24.text:='';
    25: a25.text:='';
  end;
end;

procedure TFormEvents.LoadStatusEvents;
var action, sound: string;
begin
  if EditGroup.text = '' then exit;
// 11=lHP, 12=lSP, 13=lGP1, 14=lGP2, 15=lEnemy
// 21=hHP, 22=hSP, 23=hGP1, 24=hGP2, 25=hEnemy
  LoadStatusEvent(11,action,sound);
  a11.text:=action;
  s11.hint:=sound;
  s11.text:=Datawell.FigureSound(sound);
  LoadStatusEvent(12,action,sound);
  a12.text:=action;
  s12.hint:=sound;
  s12.text:=Datawell.FigureSound(sound);
  LoadStatusEvent(13,action,sound);
  a13.text:=action;
  s13.hint:=sound;
  s13.text:=Datawell.FigureSound(sound);
  LoadStatusEvent(14,action,sound);
  a14.text:=action;
  s14.hint:=sound;
  s14.text:=Datawell.FigureSound(sound);
  LoadStatusEvent(15,action,sound);
  a15.text:=action;
  s15.hint:=sound;
  s15.text:=Datawell.FigureSound(sound);
  LoadStatusEvent(21,action,sound);
  a21.text:=action;
  s21.hint:=sound;
  s21.text:=Datawell.FigureSound(sound);
  LoadStatusEvent(22,action,sound);
  a22.text:=action;
  s22.hint:=sound;
  s22.text:=Datawell.FigureSound(sound);
  LoadStatusEvent(23,action,sound);
  a23.text:=action;
  s23.hint:=sound;
  s23.text:=Datawell.FigureSound(sound);
  LoadStatusEvent(24,action,sound);
  a24.text:=action;
  s24.hint:=sound;
  s24.text:=Datawell.FigureSound(sound);
  LoadStatusEvent(25,action,sound);
  a25.text:=action;
  s25.hint:=sound;
  s25.text:=Datawell.FigureSound(sound);
//Load Options
  CheckEventSounds.checked:=Datawell.TableOptions.fieldbyname('EventSounds').asboolean;
  CheckLowerEvent.checked:=Datawell.TableOptions3.fieldbyname('L15').asboolean;
end;

procedure TFormEvents.LoadStatusEvent(kind: integer; var action: string; var sound: string);
const
   EVENT_FILTER = '(Master = %s) and (FGroup = %s) and (Kind = %d)';
begin
  action:=''; sound:='';
  with Datawell.TableSEvents do begin
    filter := format(EVENT_FILTER, [Datawell.masterindex, quotedStr(EditGroup.Text), kind]);
    filtered := true;
    active:=true;
  end;
  action:=Datawell.TableSEvents.fieldbyname('Action').asstring;
  sound:=Datawell.TableSEvents.fieldbyname('Sound').asstring;
end;

procedure TFormEvents.SaveStatusEvents;
const
   EVENT_FILTER = '(Master = %s) and (FGroup = %s)';
begin
  with Datawell.TableSEvents do begin
    filter := format(EVENT_FILTER, [Datawell.masterindex, quotedStr(EditGroup.Text)]);
    filtered := true;
    active:=true;
    first;
    while not EOF do delete;
  end;
  SaveStatusEvent(11,a11.text,s11.hint);
  SaveStatusEvent(12,a12.text,s12.hint);
  SaveStatusEvent(13,a13.text,s13.hint);
  SaveStatusEvent(14,a14.text,s14.hint);
  SaveStatusEvent(15,a15.text,s15.hint);
  SaveStatusEvent(21,a21.text,s21.hint);
  SaveStatusEvent(22,a22.text,s22.hint);
  SaveStatusEvent(23,a23.text,s23.hint);
  SaveStatusEvent(24,a24.text,s24.hint);
  SaveStatusEvent(25,a25.text,s25.hint);
//Save Options
  Datawell.TableOptions.edit;
  Datawell.TableOptions.fieldbyname('EventSounds').asboolean:=CheckEventSounds.checked;
  Datawell.TableOptions.post;
  Datawell.TableOptions3.edit;
  Datawell.TableOptions3.fieldbyname('L15').asboolean:=CheckLowerEvent.checked;
  Datawell.TableOptions3.post;
end;

procedure TFormEvents.SaveStatusEvent(kind: integer; action, sound: string);
begin
  if (action = '') and (sound = '') then exit;
  Datawell.TableSEvents.append;
  Datawell.TableSEvents.fieldbyname('Master').asinteger:=Datawell.masterinteger;
  Datawell.TableSEvents.fieldbyname('FGroup').asstring:=EditGroup.text;
  Datawell.TableSEvents.fieldbyname('Kind').asinteger:=kind;
  Datawell.TableSEvents.fieldbyname('Action').asstring:=action;
  Datawell.TableSEvents.fieldbyname('Sound').asstring:=sound;
  Datawell.TableSEvents.post;
end;

procedure TFormEvents.BtnHelpClick(Sender: TObject);
begin
  Datawell.Help('events');
end;

procedure TFormEvents.FormShow(Sender: TObject);
var didfind: boolean;
begin
  LoadStatusEvents;
  with Datawell.TableEvents do begin
    didfind:=false;
    first;
    while not EOF do begin
      if Datawell.TableEvents.fieldbyname('FEvent').asstring = Datawell.lastevent then begin
        didfind:=true;
        break;
      end;
      next;
    end;
    if not didfind then first;
  end;
end;

procedure TFormEvents.BtnPriorClick(Sender: TObject);
begin
  CheckSave;
  SaveStatusEvents;
  case TBitBtn(sender).tag of
    -1: Datawell.TableGroups.prior;
     1: Datawell.TableGroups.next;
  end;
end;

procedure TFormEvents.DSGroupsDataChange(Sender: TObject; Field: TField);
const
   EVENT_FILTER = '(Master = %s) and (FGroup = %s)';
begin
  with Datawell.TableEvents do begin
    filter := format(EVENT_FILTER, [Datawell.masterindex, quotedStr(EditGroup.Text)]);
    filtered := true;
    active:=true;
  end;
  LoadStatusEvents;
end;

procedure TFormEvents.TabEventsChanging(Sender: TObject;
  var AllowChange: Boolean);
begin
  CheckSave;
  if TabEvents.activepage = PageEvents then LoadStatusEvents
  else SaveStatusEvents;
end;

procedure TFormEvents.FormKeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
  if key = VK_DELETE then begin
    if activecontrol = GridEvents then begin
      BtnDeleteEvent.click;
      key:=0;
    end;
  end;
end;

procedure TFormEvents.DSEventsDataChange(Sender: TObject; Field: TField);
var work: string;
begin
  if bypass then exit;
  work:=Datawell.TableEvents.fieldbyname('FAction').asstring;
  if copy(work,1,1) = '|' then begin
    delete(work,1,1);
    BtnEnable.show;
    BtnDisable.hide;
  end
  else begin
    BtnEnable.hide;
    BtnDisable.show;
  end;
  EditVars.text:=work;
  if EditVars.text = '' then begin
    LabelVars.visible:=false;
    EditVars.visible:=false;
    Script.height:=256;
    Script.top:=31;
    LabelLines.top:=15;
    Label1.top:=LabelLines.top;
  end
  else begin
    LabelVars.visible:=true;
    EditVars.visible:=true;
    Script.height:=231;
    Script.top:=56;
    LabelLines.top:=39;
    Label1.top:=LabelLines.top;
  end;
  Script.lines.assign(Datawell.TableEvents.fieldbyname('Script'));
  TimerPointer.enabled:=true;
  BtnUndo.enabled:=false;
end;

procedure TFormEvents.BtnClearBeepClick(Sender: TObject);
begin
  case (sender as TBitBtn).tag of
    11: begin s11.text:=''; s11.hint:=''; end;
    12: begin s12.text:=''; s12.hint:=''; end;
    13: begin s13.text:=''; s13.hint:=''; end;
    14: begin s14.text:=''; s14.hint:=''; end;
    15: begin s15.text:=''; s15.hint:=''; end;
    21: begin s21.text:=''; s21.hint:=''; end;
    22: begin s22.text:=''; s22.hint:=''; end;
    23: begin s23.text:=''; s23.hint:=''; end;
    24: begin s24.text:=''; s24.hint:=''; end;
    25: begin s25.text:=''; s25.hint:=''; end;
  end;
end;

procedure TFormEvents.UpdatePointer;
var
  h2: integer;
begin
  if EditVars.visible then
    h2:=32
  else h2:=8;
  DrawArrow(TPortalGrid(GridEvents), ImagePointer, 300, 10, h2);
end;

procedure TFormEvents.TimerPointerTimer(Sender: TObject);
begin
  UpdatePointer;
  TimerPointer.enabled:=false;
end;

procedure TFormEvents.BtnUpClick(Sender: TObject);
var
  h, h2: integer;
  oldevent, newevent: string;
  table: TClientDataset;
begin
  CheckSave;
  table:=TClientDataset.create(self);
  try
    table.cloneCursor(Datawell.TableEvents, false, true);
    table.IndexFieldNames := 'FOrder';
    table.Locate('FEvent', Datawell.TableEvents.fieldbyname('FEvent').asstring, []);

    oldevent:=table.fieldbyname('FEvent').asstring;
    h2:=table.fieldbyname('FOrder').asinteger;
    if TBitBtn(sender).tag = -1 then
      table.prior
    else table.next;
    if table.fieldbyname('FEvent').asstring = oldevent then exit;
    newevent:=table.fieldbyname('FEvent').asstring;
    h:=table.fieldbyname('FOrder').asinteger;
    if TBitBtn(sender).tag = -1 then
      table.next
    else table.prior;
    table.edit;
    table.fieldbyname('FOrder').asinteger:=9999;
    table.post;

    table.locate('FEvent', newEvent, []);

    table.edit;
    table.fieldbyname('FOrder').asinteger:=h2;
    table.post;

    table.locate('FEvent', oldEvent, []);

    table.edit;
    table.fieldbyname('FOrder').asinteger:=h;
    table.post;

    Datawell.TableEvents.locate('FEvent', table.fieldbyname('FEvent').asstring, []);

    TimerPointer.enabled:=false;
    TimerPointer.enabled:=true;
  finally
    table.free;
  end;
end;

procedure TFormEvents.BtnQuickEventClick(Sender: TObject);
var form: TFormEventQ;
begin
  CheckSave;
  form:=TFormEventQ.create(self);
  form.showmodal;
  form.free;
end;

procedure TFormEvents.PopupDisableClick(Sender: TObject);
var work: string;
begin
  work:=Datawell.TableEvents.fieldbyname('FAction').asstring;
  if copy(Datawell.TableEvents.fieldbyname('FAction').asstring,1,1) = '|' then
    delete(work,1,1)
  else work:='|'+work;
  Datawell.TableEvents.edit;
  Datawell.TableEvents.fieldbyname('FAction').asstring:=work;
  Datawell.TableEvents.post;
end;

procedure TFormEvents.BtnDisableClick(Sender: TObject);
var work: string;
begin
  if Datawell.TableEvents.fieldbyname('FEvent').asstring = '' then exit;
  work:=Datawell.TableEvents.fieldbyname('FAction').asstring;
  if copy(Datawell.TableEvents.fieldbyname('FAction').asstring,1,1) = '|' then
    delete(work,1,1)
  else work:='|'+work;
  Datawell.TableEvents.edit;
  Datawell.TableEvents.fieldbyname('FAction').asstring:=work;
  Datawell.TableEvents.post;
end;

procedure TFormEvents.ScriptChange(Sender: TObject);
begin
  LabelLines.caption:='Commands = '+inttostr(Script.lines.count);
  BtnUndo.enabled:=true;
end;

procedure TFormEvents.BtnUndoClick(Sender: TObject);
begin
  if TabEvents.activepage <> PageEvents then exit;
  DSEventsDataChange(Sender,nil);
  Script.setfocus;
end;

procedure TFormEvents.BtnFunctionsClick(Sender: TObject);
var
  form: TFormFunctions;
  i, po: integer;
begin
  if TabEvents.activepage <> PageEvents then exit;
  form:=TFormFunctions.create(self);
  form.Setup(Datawell.lastfunction,Script.seltext);
  form.showmodal;
  po:=form.po;
  form.free;
  i:=Script.selstart;
  if po < 1 then begin
    Script.setfocus;
    exit;
  end;
  Script.pastefromclipboard;
  Script.setfocus;
  Script.sellength:=0;
  Script.selstart:=i+po;
end;

procedure TFormEvents.BtnVarClick(Sender: TObject);
var
  formv: TFormVarGrab;
  grab: string;
  i, l: integer;
begin
  if TabEvents.activepage <> PageEvents then exit;
  formv:=TFormVarGrab.create(self);
  formv.setup;
  formv.showmodal;
  grab:=formv.grab;
  formv.free;
  i:=Script.selstart;
  l:=Script.sellength;
  if grab = '' then begin
    Script.setfocus;
    Script.selstart:=i;
    Script.sellength:=l;
    exit;
  end;
  EditCopy.text:=grab;
  EditCopy.selectall;
  EditCopy.copytoclipboard;
  Script.pastefromclipboard;
  Script.setfocus;
  Script.sellength:=0;
  Script.selstart:=i+length(EditCopy.text);
end;

procedure TFormEvents.BtnClearClick(Sender: TObject);
begin
  if Script.lines.count < 1 then exit;
  Script.lines.clear;
  BtnUndo.enabled:=true;
  Script.setfocus;
end;

procedure TFormEvents.GridEventsEnter(Sender: TObject);
begin
  CheckSave;
end;

procedure TFormEvents.CheckSave;
var
  i: integer;
begin
  with Datawell.TableEvents do begin
    if fieldbyname('FEvent').asstring <> '' then begin
      for i:=0 to Script.lines.count-1 do begin
        if pos(fieldbyname('FEvent').asstring,Script.lines[i]) > 0 then
          break;
      end;
      bypass:=true;
      edit;
      fieldbyname('Script').assign(Script.lines);
      post;
      bypass:=false;
      BtnUndo.enabled:=false;
    end;
  end;
end;

procedure TFormEvents.GridEventsKeyPress(Sender: TObject; var Key: Char);
begin
  if key = #13 then begin
    key:=#0;
    BtnEdit.click;
  end;
end;

procedure TFormEvents.GridEventsDblClick(Sender: TObject);
begin
  BtnEdit.click;
end;

procedure TFormEvents.BtnAddEventClick(Sender: TObject);
var
  form: TFormNewEvent;
  work: string;
begin
  CheckSave;
  if not Datawell.CheckRinged2('event creation/edit') then exit;
  if (TComponent(sender).tag = 0) and (CountEvents(EditGroup.text) >=25) then begin
    messagedlg('Group '+EditGroup.text+' is already filled with 25 events',mtinformation,[mbok],0);
    exit;
  end;
  if (TComponent(sender).tag = 1) and (Datawell.TableEvents.fieldbyname('FEvent').asstring = '') then exit;
  form:=TFormNewEvent.create(self);
  form.EditGroup.text:=EditGroup.text;
  if TComponent(sender).tag = 1 then begin
    form.BtnSave.show;
    form.BtnNew.hide;
    form.EditGroup.color:=clsilver;
    form.EditGroup.enabled:=false;
    form.BtnGroups.enabled:=false;
    form.EditEvent.text:=Datawell.TableEvents.fieldbyname('FEvent').asstring;
    work:=Datawell.TableEvents.fieldbyname('FAction').asstring;
    if copy(work,1,1) = '|' then begin
      form.CheckDisabled.checked:=true;
      delete(work,1,1);
    end
    else form.CheckDisabled.checked:=false;
    form.EditVariables.text:=work;
    form.EditSound.text:=Datawell.TableEvents.fieldbyname('Sound').asstring;
    form.EditSound.hint:=Datawell.TableEvents.fieldbyname('Soundfile').asstring;
    form.CheckCase.checked:=Datawell.TableEvents.fieldbyname('CaseSensitive').asboolean;
    form.CheckFiltered.checked:=Datawell.TableEvents.fieldbyname('Filtered').asboolean;
    form.CheckRetain.checked:=Datawell.TableEvents.fieldbyname('FRetain').asboolean;
    form.CheckTerminal.checked:=Datawell.TableEvents.fieldbyname('Terminal').asboolean;
    if (Datawell.TableEvents.fieldbyname('FColor').asinteger = 0) and
      (Datawell.TableEvents.fieldbyname('BColor').asinteger = 0) then begin
      form.EventText.font.color:=clblack;
      form.EventText.color:=clbtnface;
      form.CheckOriginal.checked:=true;
    end
    else begin
      form.EventText.font.color:=Datawell.TableEvents.fieldbyname('FColor').asinteger;
      form.EventText.color:=Datawell.TableEvents.fieldbyname('BColor').asinteger;
      form.CheckOriginal.checked:=false;
    end;
  end;
  form.showmodal;
  form.free;
  Script.setfocus;
  TimerPointer.enabled:=true;
  GridEvents.refresh;
end;

procedure TFormEvents.BtnGroupsClick(Sender: TObject);
var form: TFormGroups;
begin
  CheckSave;
  form:=TFormGroups.create(self);
  form.frommain:=false;
  form.showmodal;
  form.free;
end;

procedure TFormEvents.BtnAMClick(Sender: TObject);
var
  form: TFormAM;
  grab: string;
  i, l: integer;
begin
  if TabEvents.activepage <> PageEvents then exit;
  form:=TFormAM.create(self);
  form.setup;
  form.showmodal;
  grab:=form.am;
  form.free;
  i:=Script.selstart;
  l:=Script.sellength;
  if grab = '' then begin
    Script.setfocus;
    Script.selstart:=i;
    Script.sellength:=l;
    exit;
  end;
  EditCopy.text:=grab;
  EditCopy.selectall;
  EditCopy.copytoclipboard;
  Script.pastefromclipboard;
  Script.setfocus;
  Script.sellength:=0;
  Script.selstart:=i+length(EditCopy.text);
end;

procedure TFormEvents.BtnVariablesClick(Sender: TObject);
var form: TFormVars;
begin
  form:=TFormVars.create(self);
  form.nodelete:=true;
  form.BtnDelete.enabled:=false;
  form.showmodal;
  form.free;
end;

procedure TFormEvents.PopupBASEClick(Sender: TObject);
var
  amt, highorder: integer;
  didfind: boolean;
  newgroup: string;
  table: TClientDataset;
begin
  amt:=0;
  highorder:=0;
  newgroup:= stringReplace(TMenuItem(sender).caption, '&', '', []);
  didfind:=false;
  table:=TClientDataset.create(self);
  with table do begin
    cloneCursor(Datawell.TableEvents, false);
    filter := '(Master = '+Datawell.masterindex+') and (FGroup = '''+newgroup+''')';
    filtered := true;
    active:=true;
    first;
    while not EOF do begin
      if fieldbyname('FEvent').asstring = Datawell.TableEvents.fieldbyname('FEvent').asstring then
        didfind:=true;
      if fieldbyname('FOrder').asinteger > highorder then
        highorder:=fieldbyname('FOrder').asinteger;
      inc(amt);
      next;
    end;
  end;
  table.free;
  if didfind then begin
    messagedlg('That event already exists in group: '+newgroup,mtinformation,[mbok],0);
    exit;
  end;
  if amt >= 25 then begin
    messagedlg('Group '+newgroup+' is already filled with 25 events',mtinformation,[mbok],0);
    exit;
  end;
  inc(highorder);
  Datawell.TableEvents.edit;
  Datawell.TableEvents.fieldbyname('FGroup').asstring:=newgroup;
  Datawell.TableEvents.fieldbyname('FOrder').asinteger:=highorder;
  Datawell.TableEvents.post;
end;

procedure TFormEvents.PopupEventPopup(Sender: TObject);
var
  amt: integer;
  table: TClientDataset;
  g: TMenuItem;
begin
//Check disabling of event
  PopupDisable.caption:='Disable Event';
  PopupDisable.enabled:=not (Datawell.TableEvents.fieldbyname('FEvent').asstring = '');
  if copy(Datawell.TableEvents.fieldbyname('FAction').asstring,1,1) = '|' then
    PopupDisable.caption:='Enable Event';
//Populate groups
  PopupBase.visible:=true;
  g1.visible:=false;
  g2.visible:=false;
  g3.visible:=false;
  g4.visible:=false;
  g5.visible:=false;
  g6.visible:=false;
  g7.visible:=false;
  g8.visible:=false;
  g9.visible:=false;
  g10.visible:=false;
  g11.visible:=false;
  g12.visible:=false;
  g13.visible:=false;
  g14.visible:=false;
  g15.visible:=false;
  g16.visible:=false;
  g17.visible:=false;
  g18.visible:=false;
  g19.visible:=false;
  g20.visible:=false;
  N1.visible:=false;
  table:=TClientDataset.create(self);
  with table do begin
    CloneCursor(Datawell.TableGroups, true);
    filtered := true;
    active:=true;
    first;
    amt:=0;
    while not EOF do begin
      if amt > 19 then break;
      if fieldbyname('FGroup').asstring = 'BASE' then begin
        next;
        continue;
      end;
      if fieldbyname('FGroup').asstring = EditGroup.text then begin
        next;
        continue;
      end;
      case amt of
        0: g:=g1;
        1: g:=g2;
        2: g:=g3;
        3: g:=g4;
        4: g:=g5;
        5: g:=g6;
        6: g:=g7;
        7: g:=g8;
        8: g:=g9;
        9: g:=g10;
       10: g:=g11;
       11: g:=g12;
       12: g:=g13;
       13: g:=g14;
       14: g:=g15;
       15: g:=g16;
       16: g:=g17;
       17: g:=g18;
       18: g:=g19;
       19: g:=g20;
       else g := nil; //should not happen
      end;
      g.visible:=true;
      g.caption:=fieldbyname('FGroup').asstring;
      inc(amt);
      next;
    end;
  end;
  table.free;
  if amt > 0 then N1.visible:=true;
  if EditGroup.text = 'BASE' then begin
    PopupBase.visible:=false;
    N1.visible:=false;
  end;
  PopupGroup.visible:=((amt > 0) or (EditGroup.text <> 'BASE'));
  PopupGroup.enabled:=PopupDisable.enabled;
end;

function TFormEvents.CountEvents(grp: string): integer;
var table: TClientDataset;
begin
  result:=0;
  table:=TClientDataset.create(self);
  with table do begin
    cloneCursor(Datawell.TableEvents, false);
    filter := '(Master = '+Datawell.masterindex+') and (FGroup = '''+grp+''')';
    filtered := true;
    active:=true;
    first;
    while not EOF do begin
      inc(result);
      next;
    end;
  end;
  table.free;
end;

procedure TFormEvents.BtnCopyClick(Sender: TObject);
var
  group, tevent, taction, tsound, tsoundfile: string;
  tfcolor, tbcolor, torder, amt: integer;
  tcasesensitive, tfiltered, tretain, didfind: boolean;
  form: TFormSelectGroup;
  table: TClientDataset;
  lines: TStringList;
begin
  CheckSave;
  if Datawell.TableEvents.fieldbyname('FEvent').asstring = '' then begin
    messagedlg('No event to copy',mtinformation,[mbok],0);
    exit;
  end;
  form:=TFormSelectGroup.create(self);
  form.oldgroup:=EditGroup.text;
  form.showmodal;
  group:=form.group;
  form.free;
  if group = '' then exit;
  with Datawell.TableEvents do begin
    tevent:=fieldbyname('FEvent').asstring;
    taction:=fieldbyname('FAction').asstring;
    tsound:=fieldbyname('Sound').asstring;
    tsoundfile:=fieldbyname('Soundfile').asstring;
    tfcolor:=fieldbyname('FColor').asinteger;
    tbcolor:=fieldbyname('BColor').asinteger;
    tcasesensitive:=fieldbyname('CaseSensitive').asboolean;
    tfiltered:=fieldbyname('Filtered').asboolean;
    tretain:=fieldbyname('FRetain').asboolean;
    lines:=TSTringlist.create;
    lines.assign(fieldbyname('Script'));
    //Find the currently highest order, amt and existing event for the new group
    amt:=0;
    torder:=0;
    table:=TClientDataset.create(self);
    with table do begin
      cloneCursor(Datawell.TableEvents, false);
      filter := '(Master = '+Datawell.masterindex+') and (FGroup = '''+group+''')';
      filtered := true;
      active:=true;
      didfind:=false;
      first;
      while not EOF do begin
        inc(amt);
        if fieldbyname('FOrder').asinteger > torder then torder:=fieldbyname('FOrder').asinteger;
        if fieldbyname('FEvent').asstring = tevent then didfind:=true;
        next;
      end;
    end;
    table.free;
    //Add 1 to get the new highest order
    inc(torder);
    if didfind then begin
      messagedlg('An event "'+tevent+'" already exists in that group',mtinformation,[mbok],0);
      exit;
    end;
    if amt > 25 then begin
      messagedlg('Group '+group+' is full with 25 events already defined',mtinformation,[mbok],0);
      exit;
    end;
    append;
    fieldbyname('Master').asinteger:=Datawell.masterinteger;
    fieldbyname('FGroup').asstring:=group;
    fieldbyname('FEvent').asstring:=tevent;
    fieldbyname('FAction').asstring:=taction;
    fieldbyname('Sound').asstring:=tsound;
    fieldbyname('Soundfile').asstring:=tsoundfile;
    fieldbyname('FColor').asinteger:=tfcolor;
    fieldbyname('BColor').asinteger:=tbcolor;
    fieldbyname('CaseSensitive').asboolean:=tcasesensitive;
    fieldbyname('Filtered').asboolean:=tfiltered;
    fieldbyname('FRetain').asboolean:=tretain;
    fieldbyname('FOrder').asinteger:=torder;
    fieldbyname('Script').assign(lines);
    post;
  end;
  messagedlg('Event "'+tevent+' copied to group '+group,mtinformation,[mbok],0);
end;

end.
